package br.ucb.rastreador.visao.telas;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import br.ucb.rastreador.comum.PropriedadesUtil;
import br.ucb.rastreador.controle.RastroControlador;
import br.ucb.rastreador.modelo.entidade.CasoTeste;
import br.ucb.rastreador.modelo.entidade.PassoCasoTeste;
import br.ucb.rastreador.negocio.BoFactory;
import br.ucb.rastreador.negocio.CasoTesteBo;

@SuppressWarnings("serial")
public class PainelCaptura extends JPanel {
	private static final PropriedadesUtil strings = PropriedadesUtil.getStringsLocais();
	private JButton botaoIniciar;
	private JButton botaoFinalizar;
	private JButton botaoProximoPasso;
	
	private JComboBox comboCasoTeste;
	private JLabel labelCasoTeste;
	private JTable tabelaPassos;

	private JPanel panelPassos;
	private JScrollPane scrollPassos;
	
	private JPanel painelSul;
	private JPanel painelNorte;
	private JPanel painelCentral;
	
	private JDialog pai;
	
	/**
	 * 
	 * @param _pai
	 */
	public PainelCaptura(JDialog _pai){
		super();
		this.pai = _pai;
		inicializarComponentes();
	}

	/**
	 * 
	 *
	 */
	private void inicializarComponentes(){
		painelNorte = new JPanel();
		painelSul= new JPanel();
		painelCentral = new JPanel();
		labelCasoTeste = new JLabel("Caso de Teste");
		panelPassos = new JPanel();
		tabelaPassos = new JTable();
		scrollPassos = new JScrollPane();
		
		this.setLayout(new BorderLayout());
		
		this.add(painelNorte, BorderLayout.NORTH);
		this.add(painelCentral, BorderLayout.CENTER);
		this.add(painelSul, BorderLayout.SOUTH);
		
		painelNorte.add(labelCasoTeste);		
		painelNorte.add(getComboCasoTeste());	
		
		
		panelPassos.setLayout(new GridLayout(1,0));
//		panelPassos.setBorder(new TitledBorder("Passos"));
		tabelaPassos.setModel(new DefaultTableModel(
				new Object[][]{},
				new String[]{"Passo"}
				){}
		);
		tabelaPassos.getColumnModel().getColumn(0).setPreferredWidth(60);	
		
		scrollPassos.setViewportView(tabelaPassos);
		panelPassos.setBounds(10, 10, 370, 230);
		panelPassos.add(scrollPassos);
		
		painelCentral.add(panelPassos);		

		
		
		painelSul.add(getBotaoIniciar());
		painelSul.add(getBotaoProximoPasso());
		painelSul.add(getBotaoFinalizar());		
	}
	
	/**
	 * 
	 *
	 */
	private void cancelar(){
		this.pai.dispose();
	}

	/**
	 * 
	 * @return
	 */
	private JComboBox getComboCasoTeste(){
		if (comboCasoTeste == null){
			comboCasoTeste = new JComboBox();
			comboCasoTeste.setModel(getModel());
			comboCasoTeste.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					removerTodosPassos();
					if (comboCasoTeste.getSelectedIndex() != 0){
						CasoTeste casoTeste = (CasoTeste)comboCasoTeste.getSelectedItem();						
						adicionarPassos(casoTeste.getPassosCasoTeste());						
					}
				}
			});
		}
		return comboCasoTeste;		
	}
	
	/**
	 * 
	 * @return
	 */
	private DefaultComboBoxModel getModel(){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		CasoTesteBo bo = BoFactory.getFactory().getCasoTesteBo();
		model.addElement("Selecione");
		for(CasoTeste casoTeste : bo.listarTodos()){
			model.addElement(casoTeste);
		}
		return model;
		
	}
	
	private void adicionarPassos(Collection<PassoCasoTeste> passosCasoTeste){
		DefaultTableModel dtm = (DefaultTableModel) tabelaPassos.getModel();
		
		for(PassoCasoTeste passoCasoTeste : passosCasoTeste){
			dtm.addRow(new Object[]{passoCasoTeste.getPasso().getDescricao()});			
		}
	}
	
	private void removerTodosPassos(){		
		DefaultTableModel dtm = (DefaultTableModel) tabelaPassos.getModel();
		
		int numeroLinhas = dtm.getRowCount() - 1;
		for(int i = numeroLinhas; i >= 0; i--){
			dtm.removeRow(i);
		}
	}
	/**
	 * 
	 * @return
	 */
	private JButton getBotaoIniciar(){
		if (botaoIniciar == null){
			botaoIniciar = new JButton(strings.get("captura.botao.iniciar"));
			botaoIniciar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					CasoTeste casoTeste = (CasoTeste)comboCasoTeste.getSelectedItem();
					RastroControlador.getInstance().iniciarCaptura(casoTeste);
				}
			});
		}
		return botaoIniciar;
	}
	
	/**
	 * 
	 * @return
	 */
	private JButton getBotaoProximoPasso(){
		if(botaoProximoPasso == null){
			botaoProximoPasso = new JButton(strings.get("captura.botao.proximopasso"));
			botaoProximoPasso.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {					
					RastroControlador.getInstance().proximoPasso();
				}				
			});
		}
		return botaoProximoPasso;
	}
	
	/**
	 * 
	 * @return
	 */
	private JButton getBotaoFinalizar(){
		if(botaoFinalizar == null){
			botaoFinalizar = new JButton(strings.get("captura.botao.finalizar"));
			botaoFinalizar.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
//					TODO teste
					RastroControlador.getInstance().finalizarCaptura();
				}
			});
		}
		return botaoFinalizar;
	}
}
